package com.qxzn.app.medicalhealth.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qxzn.app.bean.TimesBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RemindTimeAdapter extends RecyclerView.Adapter<RemindTimeAdapter.ViewHolder> {

    private Activity activity;
    private List<TimesBean> dataList;
    private TimesBean item;
    private View.OnClickListener onItemClick;

    private View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onItemClick.onClick(v);
        }
    };
    private View.OnClickListener delClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dataList.remove((int)v.getTag());
            strSort(dataList);
            notifyDataSetChanged();
        }
    };

    public RemindTimeAdapter(Activity activity, List dataList, View.OnClickListener onItemClick) {
        this.activity = activity;
        this.dataList = dataList;
        this.onItemClick = onItemClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_time, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        item = dataList.get(position);
        holder.view.setTag(position);
        holder.view.setOnClickListener(itemClick);
        holder.tvTime.setText(item.getName());
        holder.tvTimes.setText("第" + (position + 1) + "次");
        holder.ivDelete.setTag(position);
        holder.ivDelete.setOnClickListener(delClick);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_time)
        TextView tvTime;
        @BindView(R2.id.tv_times)
        TextView tvTimes;
        @BindView(R2.id.iv_delete)
        ImageView ivDelete;
        private View view;

        ViewHolder(View view) {
            super(view);
            this.view = view;
            ButterKnife.bind(this, view);
        }
    }

    public void strSort(List<TimesBean> list) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).getValue() > list.get(j).getValue()) {
                    TimesBean t = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, t);
                }
            }
        }
    }

}
